package com.lyc.user.daoImpl;

import com.lyc.user.bean.User;
import com.lyc.user.dao.UserDao;
import com.lyc.user.util.DateUtil;
import com.lyc.user.util.jdbcUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;

public class UserDaoImpl implements UserDao {
/**
 * dao层，通过账户查询账户是否存在
 */
	@Override
	public User getUserByAccount(String account) {
		/**
		 * 连接数据库
		 */
		Connection connection=null;
		PreparedStatement ps=null;
		ResultSet resultSet=null;
		User user=null;
		try {
			connection=jdbcUtil.getConnection();
			String sql="select id,uid,nickName,account,pwd from usermessage where account=?";

		    ps=connection.prepareStatement(sql);
			ps.setString(1, account);

			resultSet= ps.executeQuery();
			if (resultSet.next()) {
                     user=new User();
                     user.setId(resultSet.getInt("id"));
				user.setId(resultSet.getInt("uid"));
				user.setNickName(resultSet.getString("nickName"));
				user.setAccount(resultSet.getString("account"));
				user.setPwd(resultSet.getString("pwd"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			jdbcUtil.close(resultSet, ps, connection);

		}
		return user;
	}

	/**
	 * 修改数据库中验证码,返回影响的行数
	 * @param phone
	 * @param veriCode
	 * @return
	 */
	@Override
	public int changeVeriCode(String phone, Integer veriCode) {
		Connection connection=null;
		PreparedStatement ps=null;
		int row=0;
		try {
			connection=jdbcUtil.getConnection();
			String sql="update usermessage set veriCode=?,vericodeTime=date_add(now(), interval 10 minute) where phone=?";
			ps=connection.prepareStatement(sql);
			ps.setString(1,veriCode+"");
			ps.setString(2,phone);
			row=ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			jdbcUtil.close(null,ps,connection);
		}
		return row;
	}

	/**
	 * 连接数据库，通过电话号码查询用户是否存在
	 * @param phone
	 * @return
	 */
	@Override
	public User getUserByPhone(String phone)  {
		Connection connection=null;
		PreparedStatement ps=null;
		ResultSet resultSet=null;
		User user=null;
		try {
			connection=jdbcUtil.getConnection();
			String sql="select id,phone,veriCode,vericodeTime from usermessage where phone=?";
			ps=connection.prepareStatement(sql);
			ps.setString(1,phone);
			resultSet=ps.executeQuery();
			user=new User();
			if (resultSet.next()){

				user.setId(resultSet.getInt("id"));
				user.setPhone(resultSet.getString("phone"));
				user.setVeriCode(resultSet.getString("veriCode"));
				String vericodeTime=resultSet.getString("vericodeTime");
				try {
					user.setVericodeTime(DateUtil.StringToDate(vericodeTime,"yyyy-MM-dd HH:mm:ss.SSS"));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			jdbcUtil.close(resultSet,ps,connection);
		}
		return user;
	}

	/**
	 * 用户账户注册时在数据库中插入账户和密码
	 * @param account
	 * @param pwd
	 * @return
	 */
	@Override
	public int changeAccountRegister(String account, String pwd) {

		Connection connection = null;
		PreparedStatement ps=null;
		int row =0;
		try {
			connection = jdbcUtil.getConnection();
			String sql="insert into usermessage (account,pwd) values(?,?)";
			ps = connection.prepareStatement(sql);
			ps.setString(1,account);
			ps.setString(2,pwd);
			row = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			jdbcUtil.close(null,ps,connection);
		}
		return row;
	}


	/**
	 * 用户用手机号码注册账号时，将电话号码,密码,验证码存入数据库
	 * @param phone
	 * @param veriCode
	 * @return
	 */
	@Override
	public int changePhonePwd(String phone,String veriCode) {
		Connection connection = null;
		PreparedStatement ps=null;
		int row =0;
		try {
			connection = jdbcUtil.getConnection();
			String sql="insert into usermessage (phone,veriCode) values(?,?)";
			ps = connection.prepareStatement(sql);
			ps.setString(1,phone);
			ps.setString(2,veriCode);
			row = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			jdbcUtil.close(null,ps,connection);
		}
		return row;
	}
}	